package com.jsb.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.annotation.SystemServiceLog;
import com.jsb.common.config.Const;
import com.jsb.common.config.Global;
import com.jsb.common.utils.StringUtils;
import com.jsb.mapper.CounselorMapper;
import com.jsb.mapper.LevelMapper;
import com.jsb.mapper.UserMapper;
import com.jsb.model.Counselor;
import com.jsb.model.CounselorExample;
import com.jsb.model.CounselorExample.Criteria;
import com.jsb.model.Level;
import com.jsb.model.User;
import com.jsb.model.UserExample;

@Service
public class CounselorService{
	
	@Resource
	private CounselorMapper counselorMapper;
	
	@Resource
	private UserMapper userMapper;
	
	@Resource
	private LevelMapper levelMapper;
	
	@Resource
	private SmsSendService smsSendService;
	
	public void addCounselor(Counselor counselor) {
		counselorMapper.insertSelective(counselor);
	}
	
	public void delCounselorByUuid(String uuid) {
		counselorMapper.deleteByPrimaryKey(uuid);
	}
	
	public void updateCounselor(Counselor counselor) {
		counselorMapper.updateByPrimaryKeySelective(counselor);
	}
	
	public void updateCounselorByExample(Counselor counselor,CounselorExample counselorExample){
	    counselorMapper.updateByExampleSelective(counselor,counselorExample);
	}
	
	public Counselor getCounselorByUuid(String uuid) {
		return counselorMapper.selectByPrimaryKey(uuid);
	}
	
	public List<Counselor> getPageCounselor(CounselorExample counselorExample) {
		return counselorMapper.selectPage(counselorExample);
	}
	
	public int countCounselor(CounselorExample counselorExample) {
		return counselorMapper.countByExample(counselorExample);
	}
	
	public List<Counselor> selectByExample(CounselorExample example){
	    return counselorMapper.selectByExample(example);
	}
	@SystemServiceLog(description = "咨询师审核通过")
	public String updatePass(String uuid, String id,String duty) {
		String info = "";
		Counselor selectByPrimaryKey = counselorMapper.selectByPrimaryKey(uuid);
		if (null != selectByPrimaryKey) {
			if (selectByPrimaryKey.getStatus().equals(Const.CHECK_NOPASS)) {
				info = "该用户认证未通过尚未重新上传认证信息";
				return "{\"status\":\"n\",\"info\":\"" + info + "\"}";
			}
//			if (selectByPrimaryKey.getStatus().equals(Const.CHECK_PASSED)
//					|| selectByPrimaryKey.getStatus().equals("")
//					|| selectByPrimaryKey.getStatus() == null) {
//				info = "该用户尚未认证或已通过审核";
//				return "{\"status\":\"n\",\"info\":\"" + info + "\"}";
//			}
			if ( selectByPrimaryKey.getStatus().equals("")
					|| selectByPrimaryKey.getStatus() == null) {
				info = "该用户尚未上传认证信息";
				return "{\"status\":\"n\",\"info\":\"" + info + "\"}";
			}
			CounselorExample counselorExample = new CounselorExample();
			Criteria createCriteria = counselorExample.createCriteria();
			createCriteria.andUuidEqualTo(uuid);
			Counselor counselor = new Counselor();
			counselor.setStatus(Integer.valueOf(Const.CHECK_PASSED));
			counselor.setAduitReason(null);
			if(StringUtils.isNotEmpty(id)){
				Level level = levelMapper.selectByPrimaryKey(id);
				counselor.setCounselorLevel(level.getUuid());
				counselor.setConsultMoney(level.getMoney());
			}
			
			String userid = selectByPrimaryKey.getUserPhoneNumber();
			User user = userMapper.selectByPrimaryKey(userid);
			user.setCounselorDuty(duty);
			user.setRole(Integer.valueOf(Const.ROLE_COUNSELOR));
			UserExample userExample  = new UserExample();
			com.jsb.model.UserExample.Criteria createCriteria2 = userExample.createCriteria();
			createCriteria2.andPhoneNumberEqualTo(userid);
			try {
				counselorMapper.updateByExampleSelective(counselor,
						counselorExample);                         //修改咨询师表status
				userMapper.updateByExampleSelective(user, userExample); //修改用户表role
				//发送短信通知
				String phone = userMapper.selectByPrimaryKey(userid).getPhone();
				smsSendService.sendNotify("8a216da8592b16a1015938d4129f03f0", phone, Const.SCENE_COUNSELOR_PASS, null);
			} catch (Exception e1) {
				e1.printStackTrace();
			}
			info = "审核成功";
			return "{\"status\":\"y\",\"info\":\"" + info + "\"}";
		} else {
			info = "未查询到该用户";
			return "{\"status\":\"n\",\"info\":\"" + info + "\"}";
		}

	}
	@SystemServiceLog(description = "咨询师审核不通过")
	public void updateNoPass(Counselor counselor, User user) {
		counselorMapper.updateByPrimaryKeySelective(counselor);
		userMapper.updateByPrimaryKeySelective(user);
		String phone = userMapper.selectByPrimaryKey(user.getPhoneNumber()).getPhone();
		//发送短信通知
		smsSendService.sendNotify("8a216da8592b16a1015938d4129f03f0", phone, Const.SCENE_COUNSELOR_NOPASS, new String[]{counselor.getAduitReason()});
	}
}
